﻿-- =============================================
-- Author:      Sergey Solyanik
-- Create date: 7/25/09
-- Description: Adds a document
-- =============================================
CREATE PROCEDURE [dbo].[AddDocument]
	@FileName NVARCHAR (MAX),
	@DocumentSetId INT,
	@Result INT OUTPUT
AS
    DECLARE @DocumentId int
    SET @DocumentId = (SELECT Id FROM dbo.Document
        WHERE DocumentSetId = @DocumentSetId AND FileName = @FileName)
    IF @DocumentId IS NOT NULL
    BEGIN
        SET @result = @DocumentId
        RETURN
    END
    INSERT INTO dbo.Document (DocumentSetId, FileName, IsActive)
        VALUES(@DocumentSetId, @FileName, 1)
    SET @Result = @@IDENTITY
RETURN 0